//
//  DeviceViewController.m
//  ZhongKeXinHeater
//
//  Created by Kendama on 2017/6/2.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "DeviceViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import "WiFiSendDataManager.h"

@interface DeviceViewController ()
/** 设备开机 **/
@property(nonatomic,strong) UIView *deviceOnView;
@property(nonatomic,strong) UILabel *fuelGasLabel;
@property(nonatomic,strong) UILabel *timeLabel;
@property(nonatomic,strong) UILabel *temperatureLabel;
@property(nonatomic,strong) UILabel *airQualityLabel;

/** 设备关机机 **/
@property(nonatomic,strong) UIView *deviceOffView;

/** 设定温度 **/
@property(nonatomic,strong) UILabel *settingTemeratureLabel;
@property(nonatomic,strong) UIButton *addTemeratureButton;
@property(nonatomic,strong) UIButton *minusTemeratureButton;
@property(nonatomic,strong) UISlider *temeratureSlider;

/** 设定时间 **/
@property(nonatomic,strong) UILabel *settingTimeLabel;
@property(nonatomic,strong) UIButton *addTimeButton;
@property(nonatomic,strong) UIButton *minusTimeButton;
@property(nonatomic,strong) UISlider *timeSlider;


@property(nonatomic,strong) UIButton *sleepButton;
@property(nonatomic,strong) UIButton *lockButton;
@property(nonatomic,strong) UIButton *lightButton;
@property(nonatomic,strong) UIButton *powerButton;


/** <#Description#> **/
@property(nonatomic,assign) NSInteger temerature;
@property(nonatomic,assign) NSInteger time;

/** 定时刷新 **/
//@property (nonatomic, strong)dispatch_source_t timing;
@property (nonatomic, strong)dispatch_source_t timer;
@end

@implementation DeviceViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(deviceStateChange:) name:@"DeviceStateNotification" object:nil];

}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self timingRefresh];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    dispatch_cancel(self.timer);
}

- (void)refreshBaseControlValue {
    
    [self setLeftButtonImage:[UIImage imageNamed:@"nav_icon_back"]];
    [self setRightButtonImage:[UIImage imageNamed:@"nav_icon_set"]];
    [self setNavigationTitle:self.title titleColor:[UIColor blackColor]];
    
    self.navigationBar.backgroundColor = [UIColor colorWithHexString:@"D0D0D0"];
    [self.navigationBar setLineViewHidden:YES];
    
    [self masLayoutSubviews];
    
    [self updateDeviceState];
    
}

#pragma mark -- 事件
- (void)deviceStateChange:(NSNotification *)notify
{

    NSString *content = notify.object;
    NSDictionary *dic = notify.userInfo;
    if ([dic[@"backCode"] isEqualToString:@"2000"]) { // 设备报警
        [self initDeviceStateWithString:content];
    } else if ([dic[@"backCode"] isEqualToString:@"1801"]) { // 设备状态查询
        [self initDeviceStateWithString:content];
    } else if ([dic[@"backCode"] isEqualToString:@"1802"]) { // 设置温度
        self.temerature = [[NSString hexadecimalToDecimal:content] integerValue];
        [self settingeTemeratureText];
    } else if ([dic[@"backCode"] isEqualToString:@"1803"]) { // 设置定时时间
        self.time = [[NSString hexadecimalToDecimal:content] integerValue];
        [self settingTimeText];
        self.timeLabel.text = [NSString stringWithFormat:@"剩余时间：%ih",[[NSString hexadecimalToDecimal:content] intValue]];

    } else if ([dic[@"backCode"] isEqualToString:@"1804"]) { // 设定模式
        if ([content isEqualToString:@"01"]) {
            self.sleepButton.selected = NO;
        } else if ([content isEqualToString:@"02"]) {
            self.sleepButton.selected = YES;
        }
    } else if ([dic[@"backCode"] isEqualToString:@"1805"]) { // 设定童锁
        if ([content isEqualToString:@"01"]) {
            self.lockButton.selected = YES;
        } else if ([content isEqualToString:@"02"]) {
            self.lockButton.selected = NO;
        }
    } else if ([dic[@"backCode"] isEqualToString:@"1806"]) { // 开机
        if ([content isEqualToString:@"01"]) {
            self.powerButton.selected = NO;
            [self.view bringSubviewToFront:self.deviceOnView];
            self.temeratureSlider.userInteractionEnabled = YES;
            self.timeSlider.userInteractionEnabled = YES;
        } else if ([content isEqualToString:@"02"]) {
            self.powerButton.selected = YES;
            [self.view bringSubviewToFront:self.deviceOffView];
            self.temeratureSlider.userInteractionEnabled = NO;
            self.timeSlider.userInteractionEnabled = NO;
        }
    } else if ([dic[@"backCode"] isEqualToString:@"1807"]) { // 设定时钟及星期
        
    } else if ([dic[@"backCode"] isEqualToString:@"1808"]) { // 系统复位
        
    } else if ([dic[@"backCode"] isEqualToString:@"1809"]) { // 背光控制
        if ([content isEqualToString:@"01"]) {
            self.lightButton.selected = YES;
        } else if ([content isEqualToString:@"02"]) {
            self.lightButton.selected = NO;
        }
    }
}


- (void) initDeviceStateWithString:(NSString *)string {
    if (string.length == 30) {
        NSLog(@"当前温度：%.1f",(CGFloat)[self getValueByHexString:string range:NSMakeRange(2, 4)]/10);
        self.temerature = [self getValueByHexString:string range:NSMakeRange(0, 2)];
        [self settingeTemeratureText];
        self.temeratureSlider.value = self.temerature;
        
        self.temperatureLabel.text = [NSString stringWithFormat:@"%i",(int)ceil((CGFloat)[self getValueByHexString:string range:NSMakeRange(2, 4)]/10)];
        //    NSLog(@"当前温度：%d",[self getValueByHexString:string range:NSMakeRange(2, 4)]);
        
        self.timeLabel.text = [NSString stringWithFormat:@"剩余时间：%ih",[self getValueByHexString:string range:NSMakeRange(6, 2)]];
        self.lightButton.selected = [self getBoolValueByHexString:string range:NSMakeRange(16, 2)];
        self.lockButton.selected = [self getBoolValueByHexString:string range:NSMakeRange(18, 2)];
        self.sleepButton.selected = ![self getBoolValueByHexString:string range:NSMakeRange(20, 2)];
        if ([self getBoolValueByHexString:string range:NSMakeRange(22, 2)]) {
            self.airQualityLabel.text = @"空气良";
        } else {
            self.airQualityLabel.text = @"空气差";
        }
        
        if ([self getBoolValueByHexString:string range:NSMakeRange(24, 2)]) {
            self.fuelGasLabel.text = @"天然气：安全";
            self.deviceOnView.backgroundColor = [UIColor colorWithHexString:@"#D0D0D0"];
        } else {
            self.fuelGasLabel.text = @"天然气：危险";
            self.deviceOnView.backgroundColor = [UIColor colorWithHexString:@"#C8171E"];
        }
        
        self.powerButton.selected = ![self getBoolValueByHexString:string range:NSMakeRange(26, 2)];
        if (self.powerButton.selected) {
            [self.view bringSubviewToFront:self.deviceOffView];
            self.temeratureSlider.userInteractionEnabled = NO;
            self.timeSlider.userInteractionEnabled = NO;
        } else {
            [self.view bringSubviewToFront:self.deviceOnView];
            self.temeratureSlider.userInteractionEnabled = YES;
            self.timeSlider.userInteractionEnabled = YES;
        }
        
        self.time = [self getValueByHexString:string range:NSMakeRange(28, 2)];
        [self settingTimeText];
        self.timeSlider.value = self.time;

    }

}

- (int)getValueByHexString:(NSString *)hexString range:(NSRange)range
{
    NSString *valueString = [hexString substringWithRange:range];
    int value = [[NSString hexadecimalToDecimal:valueString] intValue];
    return value;
}

- (BOOL)getBoolValueByHexString:(NSString *)hexString range:(NSRange)range {
    NSString *valueString = [hexString substringWithRange:range];
    if ([valueString isEqualToString:@"01"]) {
        return YES;
    } else if ([valueString isEqualToString:@"02"]) {
        return NO;
    } else {
        return NO;
    }
}


#pragma mark -- 定时刷新
- (void)timingRefresh {
    
    NSTimeInterval period = 20.0; //设置时间间隔
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(self.timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(self.timer, ^{
        //在这里执行事件
        [self updateDeviceState];
//        NSLog(@"定时刷新");

    });
    dispatch_resume(self.timer);

}

- (void)updateDeviceState {
    [[WiFiSendDataManager defaultService] getDeviceState:CurrentDevice.macAddress];
    
    if (![[DTCloudManager defaultJNI_iOS_SDK]getDevicesState:CurrentDevice.macAddress]) {
        MAIN((^{
            [SVProgressHUD showErrorWithStatus:@"设备已经离线"];
            [self.navigationController popViewControllerAnimated:YES];
        }));

    }
    NSLog(@"定时刷新：%i",[[DTCloudManager defaultJNI_iOS_SDK]getDevicesState:CurrentDevice.macAddress]);
}


#pragma mark -- 点击事件
- (void) onRightButtonClick:(id)sender {
    self.hidesBottomBarWhenPushed = YES;
    DeviceSettingViewController *vc = [[DeviceSettingViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didClickSleepButton {
    if (!self.powerButton.selected) {
        [self checkVibrate];
        if (self.sleepButton.selected) {
            [[WiFiSendDataManager defaultService] settingSleep:CurrentDevice.macAddress value:@"01"];
        } else {
            [[WiFiSendDataManager defaultService] settingSleep:CurrentDevice.macAddress value:@"02"];
        }
        [self updateDeviceState];

    }

}

- (void)didClickLockButton:(UILongPressGestureRecognizer *)gestureRecognizer {
    if (!self.powerButton.selected) {
        [self checkVibrate];
        if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
            if (self.lockButton.selected) {
                [[WiFiSendDataManager defaultService] settingLock:CurrentDevice.macAddress value:@"02"];
            } else {
                [[WiFiSendDataManager defaultService] settingLock:CurrentDevice.macAddress value:@"01"];
            }
        }

    }

}

- (void)didClickLightButton {
    if (!self.powerButton.selected) {
        [self checkVibrate];
        if (self.lightButton.selected) {
            [[WiFiSendDataManager defaultService] settingLight:CurrentDevice.macAddress value:@"02"];
        } else {
            [[WiFiSendDataManager defaultService] settingLight:CurrentDevice.macAddress value:@"01"];
        }
    }

}

- (void)didClickPowerButton {
    [self checkVibrate];
    if (self.powerButton.selected) {
        [[WiFiSendDataManager defaultService] settingPower:CurrentDevice.macAddress value:@"01"];
        [self updateDeviceState];

    } else {
        [[WiFiSendDataManager defaultService] settingPower:CurrentDevice.macAddress value:@"02"];
    }
}


- (void)didClickAddTemeratureButton {
    if (!self.powerButton.selected) {
        [self checkVibrate];
        if (self.temerature < 40) {
            self.temerature ++;
            [self settingeTemerature];
        }
    }

}

- (void)didClickMinusTemeratureButton {
    if (!self.powerButton.selected) {
        [self checkVibrate];
        if (self.temerature > 0) {
            self.temerature --;
            [self settingeTemerature];
        }
    }

}

- (void)didClickAddTimeButton {
    if (!self.powerButton.selected) {
        [self checkVibrate];
        if (self.time < 24) {
            self.time ++;
            [self settingTime];
        }
    }
}

- (void)didClickMinusTimeButton {
    if (!self.powerButton.selected) {
        [self checkVibrate];
        if (self.time > 0) {
            self.time --;
            [self settingTime];
        }
    }

}

- (void)onTemeratureThumb:(UISlider *)sender{
    UISlider *slider = sender;
    self.temerature = (int)slider.value;
    [self settingeTemeratureText];
}

- (void)endTemeratureThumb:(UISlider *)sender{
    [self checkVibrate];
    UISlider *slider = sender;
    self.temerature = (int)slider.value;
    [self settingeTemerature];
}

- (void)onTimeThumb:(UISlider *)sender{
    UISlider *slider = sender;
    self.time = (int)slider.value;
    [self settingTimeText];
}

- (void)endTimeThumb:(UISlider *)sender{
    [self checkVibrate];
    UISlider *slider = sender;
    self.time = (int)slider.value;
    [self settingTime];
}

- (void)settingeTemerature{
    [[WiFiSendDataManager defaultService] settingTemperature:CurrentDevice.macAddress value:[NSString decimalToHexadecimal:self.temerature]];

}

- (void)settingTime{
    [[WiFiSendDataManager defaultService] settingTiming:CurrentDevice.macAddress value:[NSString decimalToHexadecimal:self.time]];

}

- (void)settingeTemeratureText {
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
    NSString *value = [NSString stringWithFormat:@"%li℃",(long)self.temerature];
    [string attributeStrings:@[@"设定温度:",value] attributes:@[@[[UIColor blackColor]],@[[UIColor colorWithHexString:@"#C8171E"]]]];
    self.settingTemeratureLabel.attributedText = string;
    self.temeratureSlider.value = self.temerature;
}

- (void)settingTimeText {
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
    NSString *value;
    if (self.time == 0) {
        value = @"--";
    } else{
        value = [NSString stringWithFormat:@"%lih",(long)self.time];
    }
    [string attributeStrings:@[@"设定时间:",value] attributes:@[@[[UIColor blackColor]],@[[UIColor colorWithHexString:@"#C8171E"]]]];
    _settingTimeLabel.attributedText = string;
    self.timeSlider.value = self.time;

}

- (void)checkVibrate{ // 检查是否有振动
    if ([DTCEXRACT_OBJECT(@"Vibrate") isEqualToString:@"YES"] || DTCEXRACT_OBJECT(@"Vibrate") == nil) {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }
}



#pragma mark -- 懒加载
- (UIView *)deviceOnView
{
    if(!_deviceOnView)
    {
        UIView *superView = self.view;
        _deviceOnView = [[UIView alloc]init];
        _deviceOnView.backgroundColor = [UIColor colorWithHexString:@"#D0D0D0"];
//        _deviceOnView.backgroundColor = [UIColor colorWithHexString:@"#C8171E"];

        [superView addSubview:_deviceOnView];
    }
    return _deviceOnView;
}

- (UILabel *)fuelGasLabel
{
    if(!_fuelGasLabel)
    {
        UIView *superView = self.deviceOnView;
        _fuelGasLabel = [[UILabel alloc]init];
        [superView addSubview:_fuelGasLabel];
        [_fuelGasLabel setFont:[UIFont systemFontOfSize:px1080Width(42)]];
        [_fuelGasLabel setTextColor:[UIColor colorWithHexString:@"#231916"]];
        _fuelGasLabel.text = @"天然气：安全";
    }
    return _fuelGasLabel;
}

- (UILabel *)timeLabel
{
    if(!_timeLabel)
    {
        UIView *superView = self.deviceOnView;
        _timeLabel = [[UILabel alloc]init];
        [superView addSubview:_timeLabel];
        [_timeLabel setFont:[UIFont systemFontOfSize:px1080Width(42)]];
        [_timeLabel setTextColor:[UIColor colorWithHexString:@"#231916"]];
        _timeLabel.text = @"剩余时间：--h";
    }
    return _timeLabel;
}

- (UILabel *)temperatureLabel
{
    if(!_temperatureLabel)
    {
        UIView *superView = self.deviceOnView;
        _temperatureLabel = [[UILabel alloc]init];
        _temperatureLabel.textAlignment = NSTextAlignmentCenter;
        [superView addSubview:_temperatureLabel];
        [_temperatureLabel setFont:[UIFont systemFontOfSize:px1080Width(150)]];
        [_temperatureLabel setTextColor:[UIColor blackColor]];
        _temperatureLabel.text = @"--";
    }
    return _temperatureLabel;
}

- (UILabel *)airQualityLabel
{
    if(!_airQualityLabel)
    {
        UIView *superView = self.deviceOnView;
        _airQualityLabel = [[UILabel alloc]init];
        [superView addSubview:_airQualityLabel];
        _airQualityLabel.textAlignment = NSTextAlignmentCenter;
        [_airQualityLabel setFont:[UIFont systemFontOfSize:px1080Width(36)]];
        [_airQualityLabel setTextColor:[UIColor blackColor]];
        _airQualityLabel.layer.borderColor = [[UIColor blackColor]CGColor];
        _airQualityLabel.layer.cornerRadius = 8;
        _airQualityLabel.layer.borderWidth = 0.5;
        [_airQualityLabel.layer setMasksToBounds:YES];
        _airQualityLabel.text = @"空气优";
    }
    return _airQualityLabel;
}

- (UIView *)deviceOffView
{
    if(!_deviceOffView)
    {
        UIView *superView = self.view;
        _deviceOffView = [[UIView alloc]init];
        _deviceOffView.backgroundColor = [UIColor colorWithHexString:@"#D0D0D0"];
        [superView addSubview:_deviceOffView];
    }
    return _deviceOffView;
}

- (UILabel *)settingTemeratureLabel
{
    if(!_settingTemeratureLabel)
    {
        UIView *superView = self.view;
        _settingTemeratureLabel = [[UILabel alloc]init];
        [superView addSubview:_settingTemeratureLabel];
        [_settingTemeratureLabel setFont:[UIFont systemFontOfSize:px1080Width(45)]];
        [_settingTemeratureLabel setTextColor:[UIColor blackColor]];
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
        [string attributeStrings:@[@"设定温度:",@"--℃"] attributes:@[@[[UIColor blackColor]],@[[UIColor colorWithHexString:@"#C8171E"]]]];
        _settingTemeratureLabel.attributedText = string;
    }
    return _settingTemeratureLabel;
}
- (UIButton *)addTemeratureButton
{
    if(!_addTemeratureButton)
    {
        UIView *superView = self.view;
        _addTemeratureButton = [[UIButton alloc]init];
        [superView addSubview:_addTemeratureButton];
        [_addTemeratureButton setImage:[UIImage imageNamed:@"but_plus"] forState:UIControlStateNormal];
        [_addTemeratureButton setImage:[UIImage imageNamed:@"but_plus_pre"] forState:UIControlStateHighlighted];
        [_addTemeratureButton setContentMode:UIViewContentModeCenter];
        [_addTemeratureButton addTarget:self action:@selector(didClickAddTemeratureButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addTemeratureButton;
}
- (UIButton *)minusTemeratureButton
{
    if(!_minusTemeratureButton)
    {
        UIView *superView = self.view;
        _minusTemeratureButton = [[UIButton alloc]init];
        [superView addSubview:_minusTemeratureButton];
        [_minusTemeratureButton setImage:[UIImage imageNamed:@"but_reduce"] forState:UIControlStateNormal];
        [_minusTemeratureButton setImage:[UIImage imageNamed:@"but_reduce_pre"] forState:UIControlStateHighlighted];
        [_minusTemeratureButton addTarget:self action:@selector(didClickMinusTemeratureButton) forControlEvents:UIControlEventTouchUpInside];

    }
    return _minusTemeratureButton;
}
- (UILabel *)settingTimeLabel
{
    if(!_settingTimeLabel)
    {
        UIView *superView = self.view;
        _settingTimeLabel = [[UILabel alloc]init];
        [superView addSubview:_settingTimeLabel];
        [_settingTimeLabel setFont:[UIFont systemFontOfSize:px1080Width(45)]];
        [_settingTimeLabel setTextColor:[UIColor blackColor]];
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
        [string attributeStrings:@[@"设定时间:",@"--h"] attributes:@[@[[UIColor blackColor]],@[[UIColor colorWithHexString:@"#C8171E"]]]];
        _settingTimeLabel.attributedText = string;
//        _settingTimeLabel.text = @"";
    }
    return _settingTimeLabel;
}
- (UIButton *)addTimeButton
{
    if(!_addTimeButton)
    {
        UIView *superView = self.view;
        _addTimeButton = [[UIButton alloc]init];
        [superView addSubview:_addTimeButton];
        [_addTimeButton setImage:[UIImage imageNamed:@"but_plus"] forState:UIControlStateNormal];
        [_addTimeButton setImage:[UIImage imageNamed:@"but_plus_pre"] forState:UIControlStateHighlighted];
        [_addTimeButton addTarget:self action:@selector(didClickAddTimeButton) forControlEvents:UIControlEventTouchUpInside];

    }
    return _addTimeButton;
}
- (UIButton *)minusTimeButton
{
    if(!_minusTimeButton)
    {
        UIView *superView = self.view;
        _minusTimeButton = [[UIButton alloc]init];
        [superView addSubview:_minusTimeButton];
        [_minusTimeButton setImage:[UIImage imageNamed:@"but_reduce"] forState:UIControlStateNormal];
        [_minusTimeButton setImage:[UIImage imageNamed:@"but_reduce_pre"] forState:UIControlStateHighlighted];
        [_minusTimeButton addTarget:self action:@selector(didClickMinusTimeButton) forControlEvents:UIControlEventTouchUpInside];

    }
    return _minusTimeButton;
}


- (UISlider *)temeratureSlider
{
    if(!_temeratureSlider)
    {
        UIView *superView = self.view;
        _temeratureSlider = [[UISlider alloc]init];
        [superView addSubview:_temeratureSlider];
        _temeratureSlider.value = 1.0;
        _temeratureSlider.minimumValue = 0;
        _temeratureSlider.maximumValue = 40;
        [_temeratureSlider setMinimumTrackTintColor:[UIColor colorWithHexString:@"#C8171E"]];
        [_temeratureSlider setThumbImage:[UIImage imageNamed:@"slider"] forState:UIControlStateNormal];
        //滑动过程中不断触发事件
        [_temeratureSlider addTarget:self action:@selector(onTemeratureThumb:) forControlEvents:UIControlEventValueChanged];
        //滑动完成后触发事件
        [_temeratureSlider addTarget:self action:@selector(endTemeratureThumb:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _temeratureSlider;
}

- (UISlider *)timeSlider
{
    if(!_timeSlider)
    {
        UIView *superView = self.view;
        _timeSlider = [[UISlider alloc]init];
        [superView addSubview:_timeSlider];
        _timeSlider.value = 1.0;
        _timeSlider.minimumValue = 0;
        _timeSlider.maximumValue = 24;
        [_timeSlider setMinimumTrackTintColor:[UIColor colorWithHexString:@"#C8171E"]];
        [_timeSlider setThumbImage:[UIImage imageNamed:@"slider"] forState:UIControlStateNormal];
        //滑动过程中不断触发事件
        [_timeSlider addTarget:self action:@selector(onTimeThumb:) forControlEvents:UIControlEventValueChanged];
        //滑动完成后触发事件
        [_timeSlider addTarget:self action:@selector(endTimeThumb:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _timeSlider;
}



- (UIButton *)sleepButton
{
    if(!_sleepButton)
    {
        UIView *superView = self.view;
        _sleepButton = [[UIButton alloc]init];
        [superView addSubview:_sleepButton];
        [_sleepButton setImage:[UIImage imageNamed:@"icon_sleep"] forState:UIControlStateNormal];
        [_sleepButton setImage:[UIImage imageNamed:@"icon_sleep_pre"] forState:UIControlStateSelected];
        [_sleepButton addTarget:self action:@selector(didClickSleepButton) forControlEvents:UIControlEventTouchUpInside];

    }
    return _sleepButton;
}
- (UIButton *)lockButton
{
    if(!_lockButton)
    {
        UIView *superView = self.view;
        _lockButton = [[UIButton alloc]init];
        [superView addSubview:_lockButton];
        [_lockButton setImage:[UIImage imageNamed:@"icon_lock"] forState:UIControlStateNormal];
        [_lockButton setImage:[UIImage imageNamed:@"icon_lock_pre"] forState:UIControlStateSelected];
//        [_lockButton addTarget:self action:@selector(didClickLockButton) forControlEvents:UIControlEventTouchUpInside];
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didClickLockButton:)];
       longPress.minimumPressDuration = 1; //定义按的时间
       [_lockButton addGestureRecognizer:longPress];

    }
    return _lockButton;
}
- (UIButton *)lightButton
{
    if(!_lightButton)
    {
        UIView *superView = self.view;
        _lightButton = [[UIButton alloc]init];
        [superView addSubview:_lightButton];
        [_lightButton setImage:[UIImage imageNamed:@"icon_light"] forState:UIControlStateNormal];
        [_lightButton setImage:[UIImage imageNamed:@"icon_light_pre"] forState:UIControlStateSelected];
        [_lightButton addTarget:self action:@selector(didClickLightButton) forControlEvents:UIControlEventTouchUpInside];

    }
    return _lightButton;
}
- (UIButton *)powerButton
{
    if(!_powerButton)
    {
        UIView *superView = self.view;
        _powerButton = [[UIButton alloc]init];
        [superView addSubview:_powerButton];
        [_powerButton setImage:[UIImage imageNamed:@"but_home_on"] forState:UIControlStateNormal];
        [_powerButton setImage:[UIImage imageNamed:@"but_home_off"] forState:UIControlStateSelected];
        [_powerButton addTarget:self action:@selector(didClickPowerButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _powerButton;
}

#pragma mark -- 初始化控件
- (void)masLayoutSubviews{
    __weak UIView *superView = self.view;
    
    CGFloat buttonSize = px1080Width(150);
    
    [self.deviceOffView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(64);
        make.width.mas_equalTo(superView);
        make.height.mas_equalTo(px1920Height(500));
    }];
    
    UILabel *offLabel = [[UILabel alloc] init];
    offLabel.textAlignment = NSTextAlignmentCenter;
    offLabel.layer.borderColor = [[UIColor blackColor]CGColor];
    offLabel.layer.cornerRadius = 15;
    offLabel.layer.borderWidth = 0.5;
    [offLabel.layer setMasksToBounds:YES];
    offLabel.text = @"已关机";
    [self.deviceOffView addSubview:offLabel];
    
    [offLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.deviceOffView);
        make.width.mas_equalTo(px1080Width(276));
        make.height.mas_equalTo(px1920Height(110));
    }];
    
    
    [self.deviceOnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(64);
        make.width.mas_equalTo(superView);
        make.height.mas_equalTo(px1920Height(500));
    }];
    
    UIImageView *fuelGasIV = [[UIImageView alloc] init];
    [self.deviceOnView addSubview:fuelGasIV];
    fuelGasIV.image = [UIImage imageNamed:@"icon_fuel gas"];
    [fuelGasIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.deviceOnView).with.offset(px1920Height(64));
        make.left.mas_equalTo(self.deviceOnView).with.offset(px1080Width(53));
        make.width.width.mas_equalTo(px1080Width(33));
        make.height.width.mas_equalTo(px1920Height(50));
    }];
    
    [self.fuelGasLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(fuelGasIV.mas_top);
        make.left.mas_equalTo(fuelGasIV.mas_right).with.offset(px1080Width(14));
        make.right.mas_equalTo(self.deviceOnView.mas_centerX);
        make.height.mas_equalTo(px1920Height(50));
    }];
    
    UIImageView *timeIV = [[UIImageView alloc] init];
    [self.deviceOnView addSubview:timeIV];
    timeIV.image = [UIImage imageNamed:@"icon_time"];
    [timeIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.deviceOnView).with.offset(px1920Height(64));
        make.left.mas_equalTo(self.deviceOnView.mas_centerX).with.offset(px1080Width(80));
        make.width.width.mas_equalTo(px1080Width(40));
        make.height.width.mas_equalTo(px1920Height(50));
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(timeIV.mas_top);
        make.left.mas_equalTo(timeIV.mas_right).with.offset(px1080Width(14));
        make.right.mas_equalTo(self.deviceOnView).with.offset(px1080Width(53));
        make.height.mas_equalTo(px1920Height(50));
    }];
    
    [self.temperatureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.timeLabel.mas_bottom).with.offset(px1920Height(80));
        make.centerX.mas_equalTo(self.deviceOnView.mas_centerX);
        make.width.mas_equalTo(px1080Width(250));
        make.height.mas_equalTo(px1080Width(160));
        
    }];
    
    UILabel *temperature = [[UILabel alloc] init];
    temperature.font = [UIFont systemFontOfSize:px1080Width(48)];
    temperature.text = @"℃";
    [self.deviceOnView addSubview:temperature];
    [temperature mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.temperatureLabel.mas_top).with.offset(px1920Height(20));
        make.left.mas_equalTo(self.temperatureLabel.mas_right);
        make.height.mas_equalTo(px1920Height(56));
        make.width.mas_equalTo(px1080Width(65));
    }];
    
    
    [self.airQualityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.temperatureLabel.mas_bottom).with.offset(px1920Height(20));
        make.centerX.mas_equalTo(self.deviceOnView.mas_centerX);
        make.width.mas_equalTo(px1080Width(190));
        make.height.mas_equalTo(px1920Height(60));
        
    }];
    
    
    [self.settingTemeratureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.deviceOnView.mas_bottom).with.offset(px1920Height(46));
        make.left.mas_equalTo(self.deviceOnView).with.offset(px1080Width(57));
        make.right.mas_equalTo(self.deviceOnView).with.offset(-px1080Width(57));
        make.height.mas_equalTo(px1920Height(45));
    }];
    
    [self.minusTemeratureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.settingTemeratureLabel.mas_bottom).with.offset(px1920Height(40));
        make.left.mas_equalTo(self.deviceOnView.mas_left);
        make.width.mas_equalTo(buttonSize);
        make.height.mas_equalTo(buttonSize);
    }];
    
    
    [self.addTemeratureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.settingTemeratureLabel.mas_bottom).with.offset(px1920Height(40));
        make.right.mas_equalTo(self.deviceOnView);
        make.width.mas_equalTo(buttonSize);
        make.height.mas_equalTo(buttonSize);
    }];
    
    
    UIView *line1 = [[UIView alloc] init];
    line1.backgroundColor = [UIColor colorWithHexString:@"D6D6D6"];
    [superView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.minusTemeratureButton.mas_bottom).with.offset(px1920Height(42));
        make.left.right.equalTo(superView);
        make.height.mas_equalTo(1);
    }];
    
    [self.temeratureSlider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(line1.mas_bottom).with.offset(-px1920Height(89));
        make.left.mas_equalTo(self.minusTemeratureButton.mas_right).with.offset(px1080Width(21));
        make.right.mas_equalTo(self.addTemeratureButton.mas_left).with.offset(-px1080Width(21));
        make.height.mas_equalTo(px1920Height(40));
    }];
    
    UILabel *minTemeratureLabel = [[UILabel alloc] init];
    minTemeratureLabel.text = @"0℃";
    minTemeratureLabel.font = [UIFont systemFontOfSize:px1080Width(39)];
    minTemeratureLabel.textColor = [UIColor colorWithHexString:@"#9B9B9B"];
    [superView addSubview:minTemeratureLabel];
    [minTemeratureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.temeratureSlider.mas_top);
        make.left.mas_equalTo(self.temeratureSlider);
        make.height.mas_equalTo(px1920Height(39));
        make.width.mas_equalTo(px1080Width(70));
    }];
    
    UILabel *maxTemeratureLabel = [[UILabel alloc] init];
    maxTemeratureLabel.text = @"40℃";
    maxTemeratureLabel.textAlignment = NSTextAlignmentRight;
    maxTemeratureLabel.font = [UIFont systemFontOfSize:px1080Width(39)];
    maxTemeratureLabel.textColor = [UIColor colorWithHexString:@"#9B9B9B"];
    [superView addSubview:maxTemeratureLabel];
    [maxTemeratureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.temeratureSlider.mas_top);
        make.right.mas_equalTo(self.temeratureSlider);
        make.height.mas_equalTo(px1920Height(39));
        make.width.mas_equalTo(px1080Width(100));
    }];
    
    
    
    [self.settingTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line1.mas_bottom).with.offset(px1920Height(63));
        make.left.mas_equalTo(superView).with.offset(px1080Width(57));
        make.right.mas_equalTo(superView).with.offset(-px1080Width(57));
        make.height.mas_equalTo(px1920Height(45));
    }];
    
    [self.minusTimeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.settingTimeLabel.mas_bottom).with.offset(px1920Height(40));
        make.left.mas_equalTo(superView);
        make.width.mas_equalTo(buttonSize);
        make.height.mas_equalTo(buttonSize);
    }];
    
    
    [self.addTimeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.settingTimeLabel.mas_bottom).with.offset(px1920Height(40));
        make.right.mas_equalTo(superView);
        make.width.mas_equalTo(buttonSize);
        make.height.mas_equalTo(buttonSize);
    }];
    
    
    UIView *line2 = [[UIView alloc] init];
    line2.backgroundColor = [UIColor colorWithHexString:@"D6D6D6"];
    [superView addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.minusTimeButton.mas_bottom).with.offset(px1920Height(42));
        make.left.right.equalTo(superView);
        make.height.mas_equalTo(1);
    }];
    
    [self.timeSlider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(line2.mas_bottom).with.offset(-px1920Height(89));
        make.left.mas_equalTo(self.minusTimeButton.mas_right).with.offset(px1080Width(21));
        make.right.mas_equalTo(self.addTimeButton.mas_left).with.offset(-px1080Width(21));
        make.height.mas_equalTo(px1920Height(40));
    }];
    
    UILabel *minTimeLabel = [[UILabel alloc] init];
    minTimeLabel.text = @"0h";
    minTimeLabel.font = [UIFont systemFontOfSize:px1080Width(39)];
    minTimeLabel.textColor = [UIColor colorWithHexString:@"#9B9B9B"];
    [superView addSubview:minTimeLabel];
    [minTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.timeSlider.mas_top);
        make.left.mas_equalTo(self.timeSlider);
        make.height.mas_equalTo(px1920Height(39));
        make.width.mas_equalTo(px1080Width(70));
    }];
    
    UILabel *maxTimeLabel = [[UILabel alloc] init];
    maxTimeLabel.text = @"24h";
    maxTimeLabel.textAlignment = NSTextAlignmentRight;
    maxTimeLabel.font = [UIFont systemFontOfSize:px1080Width(39)];
    maxTimeLabel.textColor = [UIColor colorWithHexString:@"#9B9B9B"];
    [superView addSubview:maxTimeLabel];
    [maxTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.timeSlider.mas_top);
        make.right.mas_equalTo(self.timeSlider);
        make.height.mas_equalTo(px1920Height(39));
        make.width.mas_equalTo(px1080Width(100));
    }];
    
    
    CGFloat padding = px1080Width(123);
    NSMutableArray *views = [NSMutableArray array];
    [views addObjectsFromArray:@[self.sleepButton,self.lockButton,self.lightButton]];
    [views mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:px1080Width(160) leadSpacing:padding tailSpacing:padding];
    [views mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line2.mas_bottom).with.offset(px1920Height(48));
        UIView *blueView = (UIView *)views[0];
        make.height.mas_equalTo(blueView.mas_width);
    }];
    
    NSArray *buttonlArray = @[self.sleepButton,self.lockButton,self.lightButton];
    NSArray *titleArray = @[@"睡眠模式",@"童锁开关",@"背光开关"];
    for (int i = 0 ; i < 3 ; i++) {
        UIButton *button = buttonlArray[i];
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:px1080Width(36)];
        titleLabel.textColor = [UIColor colorWithHexString:@"#6F6F6F"];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = titleArray[i];
        [superView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(button.mas_bottom).with.offset(px1920Height(29));
            make.centerX.equalTo(button);
            make.width.mas_equalTo(px1080Width(158));
            make.height.mas_equalTo(px1920Height(36));
        }];
    }
    
    [self.powerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.lockButton.mas_bottom).with.offset(px1920Height(142));
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.height.mas_equalTo(px1920Height(150));
        make.width.mas_equalTo(px1080Width(480));
    }];
}




@end
